package cn.net.msg.service;

import java.io.UnsupportedEncodingException;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.commons.lang.StringUtils;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import cn.net.msg.dto.ProductDTO;
import cn.net.msg.model.PageConfig;
import cn.net.msg.model.Product;
import cn.net.msg.utils.Conversion;

import com.sina.sae.fetchurl.SaeFetchurl;

public class Fetch {

	public static void main(String[] args) throws XPatherException, UnsupportedEncodingException {


		
	}
	
	private String fetch(String url){
		SaeFetchurl fetchUrl = new SaeFetchurl();
		String result = fetchUrl.fetch("http://www.360buy.com/product/655009.html");
		return result;
	}
	
	private Product getProduct(String result) throws XPatherException{
		HtmlCleaner cleaner = new HtmlCleaner();
		TagNode node = cleaner.clean(result);
		// 按xpath取
		Object[] ns = node.evaluateXPath("//script");
		TagNode n = (TagNode) ns[1];
		String config=n.getText().toString();
		JSONObject jsonObject = JSONObject.fromObject(config.substring(config.indexOf('{'), config.indexOf(';')));
		PageConfig pageConfig = (PageConfig) JSONObject.toBean(jsonObject,PageConfig.class);
		
		ProductDTO productDto = pageConfig.getProduct();
		Conversion conversion = new Conversion();
		
		Product product = new Product();
		product.setId(productDto.getSkuid());
		product.setCount(0);
		product.setMaxPrice(0);
		product.setMinPrice(0);
		product.setName(conversion.unicodeToChinese((productDto.getName().replaceAll("%", "\\\\"))));
		
		return product;
	}
}
